package com.hspedu.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author enomix
 * @date 2022-06-14 23:35
 */
public class CollectionExercise {
    public static void main(String[] args) {
        Dog dog1 = new Dog("阿黄", 2);
        Dog dog2 = new Dog("德牧", 4);
        Dog dog3 = new Dog("土狗", 6);

        Collection col = new ArrayList();

        col.add(dog1);
        col.add(dog2);
        col.add(dog3);

        //for增强遍历
        for (Object dog : col) {
            System.out.println("dog = " + dog);
        }


        //Iterator迭代器遍历
        Iterator iterator = col.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println("next = " + next);
        }
    }
}


class Dog{
    private String name;
    private int age;

    public Dog() {
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}